package com.five.api.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.five.api.dao.CollectionMapper;
import com.five.api.service.CollectionService;
import com.five.entity.Collection;
import com.five.qo.ArticleCollectionQO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 收藏表 服务实现类
 * </p>
 *
 * @author star
 * @since 2021-06-16
 */
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements CollectionService {

    @Override
    public List<ArticleCollectionQO> selectCollectionByUserId(Long userId) {
        List<ArticleCollectionQO> collections = baseMapper.selectCollectionByUserId(userId);
        return collections;
    }

    @Override
    public int insertCollection(Long userId, Long articleId) {
        Collection collection = new Collection();
        collection.setArticleId(articleId);
        collection.setUserId(userId);
        return baseMapper.insert(collection);
    }

    @Override
    public Boolean checkIsCollection(Long userId, Long articleId) {
        QueryWrapper<Collection> collectionQueryWrapper = new QueryWrapper<>();
        Collection collectionWrapper = new Collection();
        collectionWrapper.setUserId(userId);
        collectionWrapper.setArticleId(articleId);
        collectionQueryWrapper.setEntity(collectionWrapper);
        Integer count = baseMapper.selectCount(collectionQueryWrapper);
        if(count > 0){
            return true;
        }
        return false;
    }

    @Override
    public int deleteCollection(Long userId, Long articleId) {
        QueryWrapper<Collection> collectionQueryWrapper = new QueryWrapper<>();
        Collection collectionWrapper = new Collection();
        collectionWrapper.setUserId(userId);
        collectionWrapper.setArticleId(articleId);
        collectionQueryWrapper.setEntity(collectionWrapper);
        return baseMapper.delete(collectionQueryWrapper);
    }
}
